(function () {
    var Bird = window.Bird = function () {
        var color = Math.floor(Math.random()*3);
        this.birdList = [
            game.imgList["bird"+color+"_0"],
            game.imgList["bird"+color+"_1"],
            game.imgList["bird"+color+"_2"]
        ];
        this.birdx = game.ctx.canvas.width*(1-0.618)+24;
        this.birdy = game.ctx.canvas.height*(1-0.618)+24;

        //翅膀状态
        this.wing = 0;
        //小鸟定时器
        this.fno = 0;
        //旋转角度
        this.angle = 0;
        //设置能量状态
        this.energy = false;
        //判断小鸟是否死亡
        this.isBirdDied = false;
        //小鸟飞行状态标记
        this.isReadyDown = false;
    };
    Bird.prototype.update = function () {

        if(!this.isBirdDied){
            this.wing++;
        }else{
            this.wing = 0;
        }

        this.fno++;
        if(this.wing>2){
            this.wing = 0;
        }
        //大地检测
        if(this.birdB>game.scene.land.y0){
            this.birdy = game.scene.land.y0;

            if(!game.scene.isBirdPipe){
                game.scene.enter(4);
            }

            this.isBirdDied = true;
            game.scene.noBirdLand = false;
            // clearInterval(game.timer);
        }else{
            if(!this.energy){
                this.angle += 0.06;
                if(this.angle>=Math.PI/2){
                    this.angle=Math.PI/2;
                }
                //标准0.6
                this.birdy += this.fno*0.4;
            }else{
                //标准15
                this.birdy -= (10-this.fno)*0.6;
                if(this.birdy<=0){
                    this.birdy = 0;
                }
                if(this.fno>=10){
                    this.energy = false;
                    this.fno = 0;
                }
            }
        }



        //设定小鸟碰撞位置
        this.birdT = this.birdy - 8;
        this.birdB = this.birdy + 8;
        this.birdL = this.birdx - 12;
        this.birdR = this.birdx + 12;

    };
    Bird.prototype.render = function () {
        game.ctx.save();
        //将坐标中心移到小鸟中心
        game.ctx.translate(this.birdx,this.birdy);
        game.ctx.rotate(this.angle);
        game.ctx.drawImage(this.birdList[this.wing],-24,-24);
        // game.ctx.fillRect(-14,-10,28,20);
        game.ctx.restore();

        // game.ctx.fillStyle = "black";
        // game.ctx.fillText(parseInt(this.birdT),this.birdx,this.birdT);
        // game.ctx.fillText(parseInt(this.birdB),this.birdx,this.birdB);
        // game.ctx.fillText(game.land.y0,this.birdx+100,game.land.y0);
        // game.ctx.fillText(parseInt(this.birdL),this.birdL-20,this.birdy);
        // game.ctx.fillText(parseInt(this.birdR),this.birdR,this.birdy);

    };
    Bird.prototype.fly = function () {
        this.fno = 0;
        this.angle = -0.5;
        this.energy = true;
    };
    Bird.prototype.ready = function () {
        game.scene.fno%10==0 && this.wing++;
        if(this.wing>2){
            this.wing = 0;
        }

        //gaibianzhuangtai
        this.birdy += this.isReadyDown ? 0.5 : -0.5;
        // if(this.isReadyDown){
        //     this.birdy += 0.5;
        // }else{
        //     this.birdy -= 0.5;
        // }
        if(this.birdy<=game.scene.birdflyheight-10 || this.birdy>=game.scene.birdflyheight+10){
            this.isReadyDown = !this.isReadyDown;
        }


    };
}());